
/*复选框 - 模拟 - 全选*/
;(function($){
    var isAll = true;
    var count = 0;
    var thisLabel = null;
    var isChecked = function(thisLabel){ return thisLabel.siblings("input[type='checkbox']").prop("checked") || thisLabel.siblings("input[type='checkbox']").attr('checked') || thisLabel.siblings("input[type='checkbox']").is(':checked') || thisLabel.siblings("input[type='checkbox']").attr('checked') == "checked";};


    /* 选中与取消选中 */
    var check = function(dom,checkbox,checked,countDom){
        var checkDom = $(dom).find("input[name='check']");
        var checkAll = $(dom).find("input[name='checkAll']");
        var checkSub = null;
        var checkLen = checkDom.length;
        if(!checked){
            checkbox.addClass('checked');
            checkbox.siblings("input[type='checkbox']").attr("checked", true).prop("checked", true);
            // 如果是全选按钮被选中
            if(checkbox.parents('.checkbox').attr('name') == 'checkAll'){
                checkDom.attr("checked", true).prop("checked", true);
                checkDom.siblings("label").addClass("checked");
                checkSub = $(dom).find("input[name='checkSub']");
                if(checkSub.length){
                    checkSub.attr("checked", true).prop("checked", true);
                    checkSub.siblings("label").addClass("checked");
                    count = checkSub.length;
                }else{
                    count = checkDom.length;
                }
                checkSub = null;
            }else if(checkbox.parents('.checkbox').attr('name') == 'check'){
                checkSub = checkbox.parents('.checkbox').siblings('.check-sublist').find("input[name='checkSub']");
                if(checkSub.length){
                    checkSub.attr("checked", true).prop("checked", true);
                    checkSub.siblings("label").addClass("checked");
                    count = count + checkSub.length;
                }else{
                    count++;
                }
                for (var j = 0; j < checkLen; j++) {
                    isAll = isAll && checkDom.eq(j).prop("checked");
                }
                checkAll.attr("checked", isAll).prop("checked", isAll);
                checkAll.removeAttr(isAll?"":"checked");
                checkAll.siblings("label").addClass(isAll?"checked":"");
                checkAll.siblings("label").removeClass(isAll?"":"checked");
                checkSub = null
                isAll = true;
            }else if(checkbox.parents('.checkbox').attr('name') == 'checkSub'){
                var checkSubAll = checkbox.parents('.check-sublist').siblings('.checkbox').find("input[name='check']");
                checkSub = checkbox.parents('.check-sublist').find("input[name='checkSub']");
                count++;
                for (var i = 0; i < checkSub.length; i++) {
                    isAll = isAll && checkSub.eq(i).prop("checked");
                }
                checkSubAll.attr("checked", isAll).prop("checked", isAll);
                checkSubAll.removeAttr(isAll?"":"checked");
                checkSubAll.siblings("label").addClass(isAll?"checked":"");
                checkSubAll.siblings("label").removeClass(isAll?"":"checked");
                isAll = true;
                for (var j = 0; j < checkLen; j++) {
                    isAll = isAll && checkDom.eq(j).prop("checked");
                }
                checkAll.attr("checked", isAll).prop("checked", isAll);
                checkAll.removeAttr(isAll?"":"checked");
                checkAll.siblings("label").addClass(isAll?"checked":"");
                checkAll.siblings("label").removeClass(isAll?"":"checked");
                checkSub = null
                isAll = true;
            }
        }else{
            checkbox.removeClass('checked');
            checkbox.siblings("input[type='checkbox']").prop("checked", false);
            checkbox.siblings("input[type='checkbox']").removeAttr("checked");
            // 如果是全选按钮被选中
            if(checkbox.parents('.checkbox').attr('name') == 'checkAll'){
                checkSub = $(dom).find("input[name='checkSub']");
                checkDom.removeAttr("checked");
                checkDom.prop("checked", false);
                checkDom.siblings("label").removeClass("checked");
                checkSub.removeAttr("checked");
                checkSub.prop("checked", false);
                checkSub.siblings("label").removeClass("checked");
                checkSub = null;
                count = 0;
            }else if(checkbox.parents('.checkbox').attr('name') == 'check'){
                checkSub = checkbox.parents('.checkbox').siblings('.check-sublist').find("input[name='checkSub']");
                if(checkSub.length){
                    checkSub.removeAttr("checked");
                    checkSub.prop("checked", false);
                    checkSub.siblings("label").removeClass("checked");
                    count = count - checkSub.length;
                }else{
                    count--;
                }
                isAll = false;
                checkAll.attr("checked", isAll).prop("checked", isAll);
                checkAll.removeAttr(isAll?"":"checked");
                checkAll.siblings("label").addClass(isAll?"checked":"");
                checkAll.siblings("label").removeClass(isAll?"":"checked");
                checkSub = null;
                isAll = true;
            }else if(checkbox.parents('.checkbox').attr('name') == 'checkSub'){
                var checkSubAll = checkbox.parents('.check-sublist').siblings('.checkbox').find("input[name='check']");
                count--;
                isAll = false;
                checkSubAll.attr("checked", isAll).prop("checked", isAll);
                checkSubAll.removeAttr(isAll?"":"checked");
                checkSubAll.siblings("label").addClass(isAll?"checked":"");
                checkSubAll.siblings("label").removeClass(isAll?"":"checked");
                checkAll.attr("checked", isAll).prop("checked", isAll);
                checkAll.removeAttr(isAll?"":"checked");
                checkAll.siblings("label").addClass(isAll?"checked":"");
                checkAll.siblings("label").removeClass(isAll?"":"checked");
                isAll = true;
            }
        }
        $(countDom).text(count);
    }

    /*重置选中个数*/
    var initCount = function(num) {
      count = num;
    }
    /*点击复选框事件*/
    var myCheckbox = function(dom,countDom,clearBtn){
        /*初始化样式*/
        var dom = $(dom).length>0?dom:'body';
        var init = function(){
            $('.checkbox:not(.disabled) label').each(function() {
                thisLabel = $(this);
                if (isChecked(thisLabel)) {
                    thisLabel.addClass('checked');
                    thisLabel.siblings("input[type='checkbox']").attr("checked", true).prop("checked", true);
                } else {
                    thisLabel.removeClass('checked');
                    thisLabel.siblings("input[type='checkbox']").prop("checked", false);
                    thisLabel.siblings("input[type='checkbox']").removeAttr("checked");
                }
            });
            var checkDom = $(dom).find('input[name=check]');
            var checkAll = $(dom).find('input[name=checkAll]');
            var checkLen = checkDom.length;
            if(checkAll.length && isChecked(checkAll)){
                checkDom.siblings('label').addClass('checked');
                checkDom.attr("checked", true).prop("checked", true);
            }
            if(checkLen){
                for (var j = 0; j < checkLen; j++) {
                    isAll = isAll && checkDom.eq(j).prop("checked");
                    count = checkDom.eq(j).prop("checked")?count+1:count;
                    //alert(isAll);
                }
                checkAll.attr("checked", isAll).prop("checked", isAll);
                checkAll.removeAttr(isAll?"":"checked");
                checkAll.siblings("label").addClass(isAll?"checked":"");
                checkAll.siblings("label").removeClass(isAll?"":"checked");
                isAll = true;
            }

        }

        init();
        $(dom).on('click','.checkbox:not(.disabled) label',function(){
            var checkbox = $(this);
            var checked = isChecked(checkbox);
            countDom = countDom?countDom:null;
            check(dom,checkbox,checked,countDom);
        });
        if (clearBtn && clearBtn !== null) {
            $(dom).on('click',clearBtn,function(){
                var allCheckbox = $(dom).find('input[type=checkbox]');
                allCheckbox.removeAttr("checked");
                allCheckbox.prop("checked", false);
                allCheckbox.siblings("label").removeClass("checked");
                count = 0;
                $(countDom).text(count);
            });
        }
    }

    $.MyCheckbox = myCheckbox;
    $.InitCount = initCount;

    /*
        案例：$.MyCheckbox("列表的父级元素","显示已选中数量的元素","清除当前选中项的按钮元素")

    */

})(jQuery)
